home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / KB / KB_SYSV.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  5KB  |  229 lines

  1. /*************************************************************************
  2.  
  3.     kb_sysv.c    Keyboard (KB) Subroutines
  4.             for Bywater Software
  5.  
  6.             Implementation for Unix SYSTEM V
  7.  
  8.             Copyright (c) 1991, Ted A. Campbell
  9.  
  10.             Bywater Software
  11.             P. O. Box 4023 
  12.             Duke Station 
  13.             Durham, NC  27706
  14.  
  15.             email: tcamp@hercules.acpub.duke.edu
  16.  
  17.     Copyright and Permissions Information:
  18.  
  19.     All U.S. and international copyrights are claimed by the
  20.     author. The author grants permission to use this code
  21.     and software based on it under the following conditions:
  22.     (a) in general, the code and software based upon it may be 
  23.     used by individuals and by non-profit organizations; (b) it
  24.     may also be utilized by governmental agencies in any country,
  25.     with the exception of military agencies; (c) the code and/or
  26.     software based upon it may not be sold for a profit without
  27.     an explicit and specific permission from the author, except
  28.     that a minimal fee may be charged for media on which it is
  29.     copied, and for copying and handling; (d) the code must be 
  30.     distributed in the form in which it has been released by the
  31.     author; and (e) the code and software based upon it may not 
  32.     be used for illegal activities. 
  33.  
  34. **************************************************************************/
  35.  
  36. #include "stdio.h"
  37. #include "fcntl.h"
  38. #include "signal.h"
  39. #include "sys/stat.h"
  40. #include "termio.h"
  41.  
  42. #ifndef    TRUE
  43. #define    TRUE    1
  44. #define    FALSE    0
  45. #endif
  46.  
  47. struct termio old_ttystruct;
  48. int    kb_tty;            /* fd for new terminal file */
  49.  
  50. #ifdef    KB_TEST
  51. main()
  52.     {
  53.     register int s;
  54.     static char tbuf[ 3 ];
  55.  
  56.     kb_init();
  57.  
  58.     tbuf[ 0 ] = 0;
  59.     while ( tbuf[ 0 ] != 0x1b )
  60.         {
  61.         s = read( kb_tty, tbuf, 1 );
  62.         printf( "The read() function returned %d \n", s );
  63.         if ( s > 0 )
  64.             {
  65.             printf( "The key is <%c> \n", tbuf[ 0 ] );
  66.             }
  67.         }
  68.  
  69.     kb_deinit();
  70.     }
  71. #endif
  72.  
  73. /*************************************************************************
  74.  
  75.     FUNCTION:       kb_init()
  76.  
  77.     DESCRIPTION:    This function should perform any initialization
  78.             necessary for the keyboard system.
  79.  
  80.     INPUT:          none.
  81.  
  82.     RETURNS:        none.
  83.  
  84. **************************************************************************/
  85.  
  86. kb_init()
  87.     {
  88.     struct termio new_ttystruct;
  89.  
  90.     if ( ioctl( 0, TCGETA, &old_ttystruct ) < 0 )  
  91.         {
  92.         fprintf( stderr, "Failed to get old tty parameters. \n" );
  93.         }
  94.  
  95.     if ( ( kb_tty = open( "/dev/tty", O_NDELAY)) == -1 )
  96.         {
  97.         fprintf( stderr, "Failed to open /dev/tty \n" );
  98.         }
  99. #ifdef    DEBUG
  100.     else
  101.         {
  102.         fprintf( stderr, "/dev/tty returns %d \n", kb_tty );
  103.         }
  104. #endif
  105.  
  106.     if ( ioctl( kb_tty, TCGETA, &new_ttystruct ) < 0 )  
  107.         {
  108.         fprintf( stderr, "Failed to get new tty parameters. \n" );
  109.         }
  110.  
  111.     new_ttystruct.c_cc[4] = 0;        /* minimum characters */
  112.     new_ttystruct.c_cc[5] = 0;        /* maximum time */
  113.     new_ttystruct.c_iflag = 0;
  114.     new_ttystruct.c_lflag = ISIG;
  115.     new_ttystruct.c_cflag &= ~CSIZE;
  116.     new_ttystruct.c_cflag |= CS8;
  117.     new_ttystruct.c_cflag &= ~PARENB;
  118.     if ( ioctl( kb_tty, TCSETA, &new_ttystruct ) < 0 )  
  119.         {
  120.         fprintf( stderr, "Failed to set new tty parameters. \n" );
  121.         }
  122.  
  123.     }
  124.  
  125.  
  126.  
  127. /*************************************************************************
  128.  
  129.     FUNCTION:       kb_deinit()
  130.  
  131.     DESCRIPTION:    This function should perform any necessary
  132.             deinitialization, that is, return the keyboard
  133.             to its default state when a Simple Software
  134.             program is to be exited.
  135.  
  136.     INPUT:          none.
  137.  
  138.     RETURNS:        none.
  139.  
  140. **************************************************************************/
  141.  
  142. kb_deinit()
  143.     {
  144.     if ( ioctl( 0, TCSETA, &old_ttystruct ) < 0 )
  145.         {
  146.         fprintf( stderr, "Failed to restore old tty parameters. \n");
  147.         }
  148.     }
  149.  
  150. /*************************************************************************
  151.  
  152.     FUNCTION:       kb_rxstat()
  153.  
  154.     DESCRIPTION:    This function determines whether a character is
  155.             ready from the console.  The function is used
  156.             especially in telecommunications programs,
  157.             where it is necessary to poll the keyboard
  158.             without locking up the program waiting for a
  159.             response.
  160.  
  161.     INPUT:          none.
  162.  
  163.     RETURNS:        The function returns 0 if no character is
  164.             available and 1 if a character is available.
  165.  
  166. **************************************************************************/
  167.  
  168. int kb_available = 0;
  169. int kb_hold = 0;
  170.  
  171. kb_rxstat()
  172.     {
  173.     register int s;
  174.     static char tbuf[3];
  175.  
  176.     if ( kb_available == TRUE )
  177.         {
  178.         return TRUE;
  179.         }
  180.     else
  181.         {
  182.         s = read( kb_tty, tbuf, 1 );
  183.         if ( s == FALSE )
  184.             {
  185.             kb_available = FALSE;
  186.             return FALSE;
  187.             }
  188.         else
  189.             {
  190.             kb_hold = tbuf[ 0 ];
  191.             kb_available = TRUE;
  192.             return TRUE;
  193.             }
  194.         }
  195.     }
  196.  
  197. /*************************************************************************
  198.  
  199.     FUNCTION:       kb_rx()
  200.  
  201.     DESCRIPTION:    This function returns a single character from
  202.             the keyboard.  If a character is not available
  203.             it waits.  The function should be able to
  204.             recognize any special keys, and return the
  205.             appropriate Simple Software KB conventions
  206.             designated for them.
  207.  
  208.     INPUT:          none.
  209.  
  210.     RETURNS:        The function returns the ASCII code for the
  211.             key pressed, or the Simple Software KB convention
  212.             (see kb.h) for a function or other special key.
  213.  
  214. **************************************************************************/
  215.  
  216. kb_rx()
  217.     {
  218.  
  219.     while( kb_rxstat() == FALSE )
  220.         {
  221.         ;
  222.         }
  223.  
  224.     kb_available = FALSE;
  225.     return kb_hold;
  226.     }
  227.  
  228.  
  229.